package com.erification.web.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.erification.web.model.Address;
import com.erification.web.model.Celephone;
import com.erification.web.model.Email;
import com.erification.web.model.TrueName;
import com.erification.web.model.User;
import com.erification.web.service.AddressService;
import com.erification.web.service.CelephoneService;
import com.erification.web.service.EmailService;
import com.erification.web.service.GroupService;
import com.erification.web.service.TrueNameService;
import com.erification.web.service.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
@Controller("deleteAction")
public class DeleteAction extends ActionSupport {
	@Resource 
	private UserService userService;
	@Resource
	private AddressService addressService;
	@Resource
	private CelephoneService celephoneService;
	@Resource
	private EmailService emailService;
	@Resource
	private TrueNameService trueNameService;
	@Resource
	private GroupService groupService;
	
	private TrueName trueName;
	private Address address;
	private Email email;
	private Celephone celephone;
	private String index;
	private String id;
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getIndex() {
		return index;
	}
	public void setIndex(String index) {
		this.index = index;
	}
	public TrueName getTrueName() {
		return trueName;
	}
	public void setTrueName(TrueName trueName) {
		this.trueName = trueName;
	}
	public Address getAddress() {
		return address;
	}
	public void setAddress(Address address) {
		this.address = address;
	}
	public Email getEmail() {
		return email;
	}
	public void setEmail(Email email) {
		this.email = email;
	}
	public Celephone getCelephone() {
		return celephone;
	}
	public void setCelephone(Celephone celephone) {
		this.celephone = celephone;
	}

	public String deleteTrueName() throws Exception {
		String username = (String) ActionContext.getContext().getSession().get("username");
		User user = userService.getUser(username);
		long user_id = user.getId();
		List list = (List) ActionContext.getContext().getSession().get("truename");
		list.remove(Integer.parseInt(index));
		ActionContext.getContext().getSession().put("truename", list);
		List<TrueName> all =  trueNameService.getAll(user_id);
		for(TrueName tn : all){
			if((tn.getFirstName().equals(trueName.getFirstName())) &&
					(tn.getLastName().equals(trueName.getLastName()))){
				trueNameService.deleteTrueName(tn.getId());
			}
		}
		return SUCCESS;
	}
	
	public String deleteCelephone() throws Exception {
		String username = (String) ActionContext.getContext().getSession().get("username");
		User user = userService.getUser(username);
		long user_id = user.getId();
		List list = (List) ActionContext.getContext().getSession().get("celephone");
		list.remove(Integer.parseInt(index));
		ActionContext.getContext().getSession().put("celephone", list);
		List<Celephone> all =  celephoneService.getAll(user_id);
		for(Celephone cele : all){
			if(cele.getCelephone().equals(celephone.getCelephone())){
				celephoneService.deleteCelephone(cele.getId());
			}
		}
		return SUCCESS;
	}
	
	public String deleteAddress() throws Exception {
		String username = (String) ActionContext.getContext().getSession().get("username");
		User user = userService.getUser(username);
		long user_id = user.getId();
		List list = (List) ActionContext.getContext().getSession().get("address");
		list.remove(Integer.parseInt(index));
		ActionContext.getContext().getSession().put("address", list);
		List<Address> all =  addressService.getAll(user_id);
		for(Address add : all){
			if(add.getAddress().equals(address.getAddress())){
				addressService.deleteAddress(add.getId());
			}
		}
		return SUCCESS;
	}
	
	public String deleteEmail() throws Exception {
		String username = (String) ActionContext.getContext().getSession().get("username");
		User user = userService.getUser(username);
		long user_id = user.getId();
		List<Email> all =  emailService.getAll(user_id);
		for(Email em : all){
			if(em.getEmail().equals(email.getEmail())){
				emailService.deleteEmail(em.getId());
			}
		}
		List list = (List) ActionContext.getContext().getSession().get("email");
		list.remove(Integer.parseInt(index));
		ActionContext.getContext().getSession().put("email", list);
		return SUCCESS;
	}
	
	public String deleteGroup() throws Exception {
		groupService.deleteGroup(Long.parseLong(id));
		List list = (List) ActionContext.getContext().getSession().get("group");
		list.remove(Integer.parseInt(index));
		ActionContext.getContext().getSession().put("group", list);
		return "group";
	}
}
